<template>
    <div class="container">
        <div class="title">
            <span>添加</span>
            <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
        </div>
        <el-divider></el-divider>
        <br/>
        <el-form label-width="120px"
                 :model="form"
                 status-icon
                 ref="form"
                 @submit.native.prevent>
            <el-form-item label="标题" prop="title">
                <el-input size="medium" v-model="form.title" placeholder="请填写标题"></el-input>
            </el-form-item>
            <el-form-item label="满额" prop="money">
                <el-input size="medium" type="number" v-model="form.money" placeholder="请填写满额金额"></el-input>
            </el-form-item>
            <el-form-item label="减" prop="price">
                <el-input size="medium" type="number" v-model="form.price" placeholder="请填写减的金额"></el-input>
            </el-form-item>
            <el-form-item label="开始时间" prop="start_time">
                <el-date-picker
                    v-model="form.start_time"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择开始时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="end_time">
                <el-date-picker
                        v-model="form.end_time"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择结束时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="数量" prop="number">
                <el-input size="medium" type="number" v-model="form.number" placeholder="请填写数量"></el-input>
            </el-form-item>
            <el-form-item class="submit">
                <el-button type="primary" @click="submitForm()">保 存</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import coupon from '@/models/coupon'

export default {
  data() {
    return {
      form: {
        title: '',
        money: '',
        price: '',
        start_time: '',
        end_time: '',
        number: '',
      },
    }
  },
  created() {
  },
  methods: {
    async submitForm() {
      const self = this
      const info = {
        title: self.form.title,
        money: self.form.money,
        price: self.form.price,
        start_time: self.form.start_time,
        end_time: self.form.end_time,
        number: self.form.number,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await coupon.createCoupon(info)
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
          this.back()
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('addClose')
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
